import { type NextRequest, NextResponse } from 'next/server';

export class PostsHandler {
    handle = async (request: NextRequest) => {
        const method = request.method.toUpperCase();
        
        // Mock data - in a real app, this would come from a database
        const posts = Array.from({ length: 20 }, (_, i) => ({
            id: i + 1,
            title: `Post ${i + 1}`,
            body: `This is the content of post ${i + 1}. It contains some sample text to demonstrate how the content would look like in a real application. This is just placeholder text for demonstration purposes.`
        }));
        
        if (method === 'GET') {
            // Simulate network delay
            await new Promise(resolve => setTimeout(resolve, 100));
            
            const { searchParams } = new URL(request.url);
            const page = parseInt(searchParams.get('page') || '1', 10);
            const limit = parseInt(searchParams.get('limit') || '5', 10);

            const startIndex = (page - 1) * limit;
            const endIndex = startIndex + limit;
            const paginatedPosts = posts.slice(startIndex, endIndex);
            
            return NextResponse.json(paginatedPosts);
        }
        
        return NextResponse.json({ error: 'Method not allowed' }, { status: 405 });
    };
}